package com.cookpan001.mediators 
{
	import com.cookpan001.App;
	import com.cookpan001.data.AppConst;
	import com.cookpan001.display.layer.MoveLayer;
	import com.cookpan001.display.layer.StaticLayer;
	import com.cookpan001.proxies.DragProxy;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import org.puremvc.as3.interfaces.INotification;
	/**
	 * 初始化主场景
	 * @author cookpan001
	 */
	public class AppMediator extends AbstractMediator
	{
		public static const NAME:String = 'AppMediator';
		
		public function AppMediator(viewComponent:Object) 
		{
			super(NAME,viewComponent);
		}
		
		public function get app():App
		{
			return this.viewComponent as App;
		}
		
		//注册后启动
		override public function onRegister():void
		{
			super.onRegister();
			this.initEvent();
		}
		//初始化鼠标事件
		private function initEvent():void
		{
			this.app.addEventListener(MouseEvent.MOUSE_DOWN, this.down, false, 0, true);
			this.app.addEventListener(MouseEvent.MOUSE_UP, this.up, false, 0, true);
		}
		
		private function down(e:MouseEvent):void
		{
			this.app.addEventListener(MouseEvent.MOUSE_MOVE, this.move, false, 0, true);
			var offset:Point = new Point(e.stageX,e.stageY);
			this.sendNotification(AppConst.START_DRAG,offset);
		}
		
		private function up(e:MouseEvent):void
		{
			this.app.removeEventListener(MouseEvent.MOUSE_MOVE, this.move);
			this.sendNotification(AppConst.END_DRAG);
		}
		
		private function move(e:MouseEvent):void
		{
			var offset:Point = new Point(e.stageX,e.stageY);
			this.sendNotification(AppConst.DRAGGING,offset);
		}
		
		//注册关心的消息
		override public function listNotificationInterests():Array
		{
			return [
				AppConst.INIT_ROOM
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);
			
			var name:String = notification.getName();
			switch(name)
			{
				case AppConst.INIT_ROOM:
					this.initRoom();
					break;
				default:
					break;
			}
		}
		
		private function initRoom():void
		{
			this.addLayer("StaticLayer", new StaticMediator(new StaticLayer()));
			this.addLayer("MoveLayer", new MoveMediator(new MoveLayer()));
		}
	}

}